package com.netty.server.processor.down;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Service;

/**
 * 台灯设置
 *
 * @author: 王霄
 * @date: 2022/2/18 19:44
 */
@Service
public class DownPlugin0X01 extends DefaultDownPlugin implements BaseData {

    @Getter
    @Setter
    private boolean switches;
    @Getter
    @Setter
    private byte brightness;


    @Override
    public byte getCmd() {
        return 0X01;
    }

    @Override
    protected byte[] serializer(BaseData data) {
        if (data instanceof DownPlugin0X01 plugin) {
            ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
            try {
                buf.writeByte(plugin.switches ? 0 : 1);
                buf.writeByte(plugin.brightness);

                byte[] bs = new byte[buf.readableBytes()];
                buf.readBytes(bs);
                return bs;
            } finally {
                buf.release();
            }
        } else {
            throw new RuntimeException();
        }
    }
}
